<template>
  <div v-if="visible" class="modal-overlay" @click.self="closeModal">
    <div class="modal-container">
      <div class="modal-header">
        <h2>AnkiConnect 配置</h2>
        <button class="close-button" @click="closeModal">×</button>
      </div>
      <div class="modal-body">
        <div class="config-form">
          <div class="form-row">
            <div class="form-group">
              <label>服务器地址：</label>
              <input v-model="localConfig.server" type="text" placeholder="192.168.2.120">
            </div>
            <div class="form-group">
              <label>端口号：</label>
              <input v-model.number="localConfig.port" type="number" placeholder="8765">
            </div>
          </div>
          <div class="form-row">
            <div class="form-group">
              <label>Deck名称：</label>
              <div class="select-with-refresh">
                <select v-model="localConfig.deckName" :disabled="isLoadingDecks">
                  <option v-if="availableDecks.length === 0" value="">无可用Deck</option>
                  <option v-for="deck in availableDecks" :key="deck" :value="deck">
                    {{ deck }}
                  </option>
                </select>
                <button @click="refreshDecks" class="refresh-button" :disabled="isLoadingDecks">
                  {{ isLoadingDecks ? '加载中...' : '刷新' }}
                </button>
              </div>
            </div>
            <div class="form-group">
              <label>Model名称：</label>
              <div class="select-with-refresh">
                <select v-model="localConfig.modelName" :disabled="isLoadingModels">
                  <option v-if="availableModels.length === 0" value="">无可用Model</option>
                  <option v-for="model in availableModels" :key="model" :value="model">
                    {{ model }}
                  </option>
                </select>
                <button @click="refreshModels" class="refresh-button" :disabled="isLoadingModels">
                  {{ isLoadingModels ? '加载中...' : '刷新' }}
                </button>
              </div>
            </div>
          </div>
          <div class="form-group">
            <label>API密钥（可选）：</label>
            <input v-model="localConfig.apiKey" type="text" placeholder="无需密钥则留空">
          </div>

          <!-- 新增：模型字段展示区域 -->
          <div class="model-fields-section" v-if="modelFields.length > 0">
            <h3>当前模型字段</h3>
            <div class="model-fields-list">
              <span v-for="(field, index) in modelFields" :key="index" class="field-tag">
                {{ field }}
              </span>
            </div>
          </div>

          <div class="config-actions">
            <button @click="saveConfig" class="primary-button">保存配置</button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'ConfigPanel',
  props: {
    config: {
      type: Object,
      required: true
    },
    visible: {
      type: Boolean,
      default: false
    },
    availableDecks: {
      type: Array,
      default: () => []
    },
    isLoadingDecks: {
      type: Boolean,
      default: false
    },
    availableModels: {
      type: Array,
      default: () => []
    },
    isLoadingModels: {
      type: Boolean,
      default: false
    },
    modelFields: {
      type: Array,
      default: () => []
    },
    isLoadingModelFields: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      localConfig: { ...this.config }
    }
  },
  watch: {
    config: {
      handler(newConfig) {
        this.localConfig = { ...newConfig };
      },
      deep: true
    },
    'localConfig.modelName'(newVal) {
      if (newVal) {
        this.$emit('model-name-changed', newVal);
      }
    }
  },
  methods: {
    saveConfig() {
      this.$emit('save-config', { ...this.localConfig });
      console.log('保存的配置：', this.localConfig)
      this.closeModal();
    },
    refreshDecks() {
      this.$emit('refresh-decks');
    },
    refreshModels() {
      this.$emit('refresh-models');
    },
    closeModal() {
      this.$emit('close');
    }
  }
}
</script>

<style scoped>
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal-container {
  background-color: white;
  border-radius: 8px;
  width: 90%;
  max-width: 600px;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px 25px;
  border-bottom: 1px solid #e9ecef;
}

.modal-header h2 {
  color: #2c3e50;
  margin: 0;
  font-size: 1.5em;
}

.close-button {
  background: none;
  border: none;
  font-size: 24px;
  cursor: pointer;
  color: #6c757d;
  padding: 0;
  width: 30px;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 4px;
}

.close-button:hover {
  background-color: #f8f9fa;
  color: #2c3e50;
}

.modal-body {
  padding: 25px;
}

.config-form {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.form-row {
  display: flex;
  gap: 20px;
  flex-wrap: wrap;
}

.form-group {
  flex: 1;
  min-width: 250px;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  font-weight: bold;
  color: #34495e;
}

.form-group input,
.form-group select {
  width: 100%;
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 14px;
  box-sizing: border-box;
}

.select-with-refresh {
  display: flex;
  gap: 10px;
}

.select-with-refresh select {
  flex: 1;
}

.refresh-button {
  padding: 10px 15px;
  background-color: #2196F3;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  white-space: nowrap;
}

.refresh-button:hover:not(:disabled) {
  background-color: #0b7dda;
}

.refresh-button:disabled {
  background-color: #ccc;
  cursor: not-allowed;
}

.config-actions {
  margin-top: 10px;
}

.primary-button {
  background-color: #4CAF50;
  color: white;
  padding: 10px 20px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  font-weight: bold;
}

.primary-button:hover {
  background-color: #45a049;
}

/* 新增：模型字段样式 */
.model-fields-section {
  margin-top: 20px;
  padding: 15px;
  background-color: #f0f7ff;
  border-radius: 8px;
  border-left: 4px solid #2196F3;
}

.model-fields-section h3 {
  margin-top: 0;
  margin-bottom: 10px;
  color: #333;
  font-size: 1.2em;
}

.model-fields-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.field-tag {
  background-color: #e3f2fd;
  color: #1976d2;
  padding: 6px 12px;
  border-radius: 16px;
  font-size: 14px;
  display: inline-block;
  font-weight: 500;
}
</style>
